layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;



    var companyId =  Feng.getUrlParam("companyId");
    /**
     * 拟实施措施计划管理
     */
    var TProposedMeasuresPlan = {
        tableId: "tProposedMeasuresPlanTable"
    };

    /**
     * 初始化表格的列
     */
    TProposedMeasuresPlan.initColumn = function () {
        return [[
            {field: 'rectificationPlan', sort: true, title: '整改计划'},
            {field: 'planTime', sort: true, title: '计划完成时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TProposedMeasuresPlan.search = function () {
        var queryData = {};
        queryData['companyName'] = $("#companyName").val();
        table.reload(TProposedMeasuresPlan.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TProposedMeasuresPlan.openAddDlg = function () {
        func.open({
            title: '添加拟实施措施计划',
            content: Feng.ctxPath + '/tProposedMeasuresPlan/add',
            tableId: TProposedMeasuresPlan.tableId
        });
    };




    /**
     * 导出excel按钮
     */
    TProposedMeasuresPlan.exportExcel = function () {
        var checkRows = table.checkStatus(TProposedMeasuresPlan.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TProposedMeasuresPlan.tableId,
        url: Feng.ctxPath + '/vocManagementLedger/ledgerInfo?companyId='+ companyId,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TProposedMeasuresPlan.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TProposedMeasuresPlan.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TProposedMeasuresPlan.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TProposedMeasuresPlan.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TProposedMeasuresPlan.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TProposedMeasuresPlan.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TProposedMeasuresPlan.onDeleteItem(data);
        }else if(layEvent === "measureInfo"){
            TProposedMeasuresPlan.openMeasureInfo(data);
        }else if(layEvent === "ledgerInfo"){
            TProposedMeasuresPlan.ledgerInfo(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        TProposedMeasuresPlan.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
    TProposedMeasuresPlan.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/tProposedMeasuresPlan/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( TProposedMeasuresPlan.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( TProposedMeasuresPlan.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/tProposedMeasuresPlan/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( TProposedMeasuresPlan.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( TProposedMeasuresPlan.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };






    /**
     * 点击提交
     *
     * @param data 点击按钮时候的行数据
     */
    TProposedMeasuresPlan.commitReport = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tProposedMeasuresPlan/commitReport", function (data) {
                if (data.code == 200) {
                    Feng.success("提交成功!");
                }else{
                    Feng.error("提交失败!" + data.message + "!");
                }
                table.reload(TProposedMeasuresPlan.tableId);
            }, function (data) {
                Feng.error("提交失败!" + data.message + "!");
            });
            ajax.set("companyId", data.companyId);
            ajax.start();
        };
        Feng.confirm("是否提交?", operation);
    };
});
